package com.zhenhua.server.process;

import java.io.IOException;

import com.common.entity.Appointment;
import com.common.entity.DataPack;
import com.common.entity.Parm;
import com.common.server.view.ServerMainFrame;
import com.common.server.view.ServerProcessThread;
import com.common.server.view.ServiceProcess;
import com.zhenhua.server.dao.IAppointmentServer;
import com.zhenhua.server.dao.impl.AppointmentServerImpl;

import utils.Page;

public class AppointmentServiceProcess implements ServiceProcess {
	private IAppointmentServer iAppointmentServer = new AppointmentServerImpl();

	@Override
	public Object service(DataPack dataPack, ServerProcessThread serverProcessThread, ServerMainFrame serverMainFrame)
			throws IOException {
		if (dataPack.getOptType().equals("add")) {
			int result = 0;
			result = iAppointmentServer.insert((Appointment) dataPack.getCon());
			serverProcessThread.getOos().writeObject(result);
			serverProcessThread.getOos().flush();
		} else if ((dataPack.getOptType().equals("showData"))) {
			Parm parm = (Parm) dataPack.getCon();
			Integer stuNumber = (Integer) parm.getList().get(0);
			int currentPage = (int) parm.getList().get(1);
			int pageSize = (int) parm.getList().get(2);
			Page<Appointment> page = iAppointmentServer.getPageByStuNumber(stuNumber, currentPage, pageSize);
			
			DataPack dp2=new DataPack();
			dp2.setOptType(dataPack.getOptType());
			dp2.setCon(page);
			serverProcessThread.getOos().writeObject(dp2);
			serverProcessThread.getOos().flush();
		}else if ((dataPack.getOptType().equals("delete"))) {
			int[] ids=(int[]) dataPack.getCon();
			boolean isYes=iAppointmentServer.deleteByIds(ids);
			serverProcessThread.getOos().writeObject(isYes);
			serverProcessThread.getOos().flush();
		}else if ((dataPack.getOptType().equals("getAppointmentById"))) {
			Appointment appointment=iAppointmentServer.getAppointmentById((int)dataPack.getCon());
			serverProcessThread.getOos().writeObject(appointment);
			serverProcessThread.getOos().flush();
		}else if ((dataPack.getOptType().equals("showDataByCoachNumber"))) {
			Parm parm = (Parm) dataPack.getCon();
			Integer coachNumber = (Integer) parm.getList().get(0);
			int currentPage = (int) parm.getList().get(1);
			int pageSize = (int) parm.getList().get(2);
			Page<Appointment> page = iAppointmentServer.getPageByCoachNumber(coachNumber, currentPage, pageSize);
			
			DataPack dp2=new DataPack();
			dp2.setOptType(dataPack.getOptType());
			dp2.setCon(page);
			serverProcessThread.getOos().writeObject(dp2);
			serverProcessThread.getOos().flush();
		}else if ((dataPack.getOptType().equals("update"))) {
			int result=iAppointmentServer.update((Appointment)dataPack.getCon());
			serverProcessThread.getOos().writeObject(result);
			serverProcessThread.getOos().flush();
		}
		return null;
	}
}
